<extend name="./Base/base" />

<block name="main">
        <div class="col-md-6">
        <div class="panel">
            <div class="panel-heading bk-bg-primary">
                <h6><i class="fa fa-indent red"></i>修改文章</h6>                            
            </div>
            <div class="panel-body">
                <form  id="form_at" method="post" enctype="multipart/form-data" name='myform' class="form-horizontal ">
                    <input type="hidden"  name="id"  value="{$list.id}" />
                    <div class="form-group">
                        <label class="col-md-3 control-label"  for="text-input">文章标题</label>
                        <div class="col-md-9">
                            <input id="text-input" name="title" onblur="validate();" class="form-control" value="{$list['title']}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="textarea-input">文章内容</label>
                        <div class="col-md-9">
                            <textarea id="textarea-input"  name="content" rows="9" class="form-control">{$list['content']}</textarea>
                        </div>
                    </div>

                    <div class="form-group">
                            <label class="col-md-3 control-label" for="textarea-input">文章配图</label>
                            <!-- <input type="file" id="inputfile" name="img" style="position:absolute;display:none;z-index:999;"> -->
                            <input type='file' id='iptfileupload' onchange='show()' name="imgs"  /><br />
                           
                            <!-- <img src="__PUBLIC__/admin/assets/img/{$list.img}" alt="" style="width:120px;higth:120px;"> -->
                            <img src="__PUBLIC__/article/{$list.img}" style="width:150px;higth:150px;margin-left:186px;" alt='' id='img' />
                        </div>
                        <!-- <input type="submit" id="login_user" class="btn btn-inverse" value="提交"> -->
                        <div id="message"  style="margin-left:115px;color:red;font-size:20px;"></div>
                    <br>
                    <button  onclick="button_cate();return false;" class="btn btn-large btn-primary" style="margin-left:35%;">&nbsp;&nbsp;提交&nbsp;&nbsp;</button>
                </form>
            </div>  
        </div>  
    </div>
</block>
<block name="script_user">
    <script type="text/javascript">
    function getPath(obj,fileQuery,transImg) 
    {
 
           var imgSrc = '', imgArr = [], strSrc = '' ;
         
           if(window.navigator.userAgent.indexOf("MSIE")>=1){ // IE浏览器判断
            if(obj.select){
             obj.select();
             var path=document.selection.createRange().text;
             alert(path) ;
             obj.removeAttribute("src");
             imgSrc = fileQuery.value ;
             imgArr = imgSrc.split('.') ;
             strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
             if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
              obj.setAttribute("src",transImg);
              obj.style.filter=
               "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path+"', sizingMethod='scale');"; // IE通过滤镜的方式实现图片显示
             }else{
              //try{
              throw new Error('File type Error! please image file upload..'); 
              //}catch(e){
              // alert('name: ' + e.name + 'message: ' + e.message) ;
              //}
             }
            }else{
             // alert(fileQuery.value) ;
             imgSrc = fileQuery.value ;
             imgArr = imgSrc.split('.') ;
             strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
             if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
              obj.src = fileQuery.value ;
             }else{
              //try{
              throw new Error('File type Error! please image file upload..') ;
              //}catch(e){
              // alert('name: ' + e.name + 'message: ' + e.message) ;
              //}
             }
         
            }
         
           } else{
            var file =fileQuery.files[0];
            var reader = new FileReader();
            reader.onload = function(e){
         
             imgSrc = fileQuery.value ;
             imgArr = imgSrc.split('.') ;
             strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
             if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
              obj.setAttribute("src", e.target.result) ;
             }else{
              //try{
              throw new Error('File type Error! please image file upload..') ;
              //}catch(e){
              // alert('name: ' + e.name + 'message: ' + e.message) ;
              //}
             }
         
             // alert(e.target.result); 
            }
            reader.readAsDataURL(file);
           }
    } 
    function show()
    {
       //以下即为完整客户端路径
       var file_img=document.getElementById("img"),
        iptfileupload = document.getElementById('iptfileupload') ;
       getPath(file_img,iptfileupload,file_img) ;
    }
    function button_cate()
    {
        var text = $("input[name='title']").val();
        var content = $("textarea[name='content']").val();
        var img = $("input[name='imgs']").val();
        if ($("#text-input").val() == '' ||$("#textarea-input").val() == ''  ) {
            $('#message').html('<p>error:文章和标题不能为空</p>');
            return false;
        };
        var form = new FormData(document.getElementById('form_at'));
        $.ajax({
            url:"{:U('Article/article_edit')}",
            type:"post",
            data:form,
            processData:false,
            contentType:false,
            success:function(data){
              if (data == 1) {
                alert('异常错误');
              }else if (data == 2) {
                 $('#message').html('<p>error:图片类型错误</p>');
              }else if (data == 3) {
                 $('#message').html('<p>error:必须上传文章配图</p>');
              }else{
                  var res = data.substr(0,1);
                  var url = data.substring(2);
                  if (res == 4) {
                      window.location.href = url;
                  }else if (res == 5) {
                    alert('添加失败,请重新添加');
                    window.location.href = url;
                  };
                }
            }
        });
        return false;
    } 
 </script>
</block>